/**
 * 功能描述
 */
define(function (require, exports, module) {
	var Grid = require("grid");
	var Dialog = require("dialog");
	
	var aw = require("ajaxwrapper");
	
    var instance = {
        initComponent: function (params, widget) {
        	var grid = new Grid({
        		parentNode : ".J-grid",
        		autoRender: false,
        		model : {
        			url : "api/checkoutdeal/query",
            		params : function(){
            			var subnav = widget.get("subnav");
            			return {
            				"checkInImplement.servicePoint": subnav.getValue("servicePoint"),
            				"status": subnav.getValue("status"), 
            				"paymentMethod": subnav.getValue("paymentMethod"), 
            				"source":"NonMember,VisitDoorFIT",
            				//"checkInImplement.dataSourceIn" : "NonMember,VisitDoorFIT",
            				"checkInImplement.nonMemGroupReserve.nonMemberGroup.checkOutDate" : subnav.getValue("checkOutDate").start,
            				"checkInImplement.nonMemGroupReserve.nonMemberGroup.checkOutDateEnd" : subnav.getValue("checkOutDate").end,
        					fetchProperties:"*,paymentMethods.*,checkInImplement.*,checkInImplement.signUpNum," +
        							"checkInImplement.nonMemGroupReserve.*," +
        							"checkInImplement.nonMemGroupReserve.nonMemberGroup.*," +
        							"checkInImplement.nonMemGroupReserve.nonMemberGroup.servicePoint.name," +
        							"checkInImplement.servicePoint.name," +
        							"checkInImplement.nonMemGroupReserve.signUpRooms.*," +
        							"checkInImplement.nonMemGroupReserve.signUpRooms.roomType.name," +
        							"checkInImplement.nonMemGroupReserve.signUpRooms.signUpRoomPrices.roomPrice," +
        							"checkInImplement.nonMemGroupReserve.signUpRooms.signUpRoomPrices.checkInPrice," +
        							"checkInImplement.nonMemGroupReserve.nonMemberGroup.operator.servicePoint.name," +
        							"operator.name," +
        							"operator.pkOperator"
            			}
            		},
        			columns : [{
        				name : "desName",
        				label : "目的地",
        				format : function(value, row){
    						return row.checkInImplement.nonMemGroupReserve.nonMemberGroup.servicePoint.name;
        				}
        			},{
        				name : "checkInImplement.nonMemGroupReserve.nonMemberGroup.checkInDate",
        				label : "入住日期",
        				format : "date"
        			},{
        				name:"checkInImplement.nonMemGroupReserve.nonMemberGroup.checkOutDate",
        				label : "退房日期",
        				format : "date"
        			},{
        				name:"checkInImplement.signUpNum",
        				label:"人数",
        			},{
        				name:"checkInImplement.nonMemGroupReserve.nonMemberGroup.tourType.value",
        				label:"散客类型",
        				format : function(value, row){
        					if(row.checkInImplement.dataSource.key == "VisitDoorFIT"){
        						return row.checkInImplement.dataSource.value;
        					}else if(row.checkInImplement.nonMemGroupReserve.nonMemberGroup.signUpType.key == "NonMember"){
        						return row.checkInImplement.nonMemGroupReserve.nonMemberGroup.tourType.value;
        					}else{
        						return "体验客户";
        					}
        				}
        			},{
        				name:"money",
        				label : "结算金额",
        				format :function(value, row){
        					return parseFloat(value).toFixed(2);
        				}
        			},{
        				name : "checkInImplement.paymentMethods",
        				label : "结算方式",
        				format : function(value, row){
        					var str = "";
        					for(var i = 0; i < row.paymentMethods.length; i++){
        						if(i == 0){
        							str = row.paymentMethods[i].value;
        						}else{
        							str += "," + row.paymentMethods[i].value
        						}
        					}
        					return str;
        				}
        			},{
        				name:"isSales",
        				label : "是否销账",
        				format :function(value, row){
        					if(row.isSales == null){
        						return "";
        					}else if(row.isSales){
        						return "已销账";
        					}else{
        						return "未销账";
        					}
        					return parseFloat(value).toFixed(2);
        				}
        			},{
        				name : "status.value",
        				label : "状态"
        			},{
        				name:"operate",
        				label:"操作",
        				format:"button",
        				formatparams:[{
        					id:"confirm",
        					text:"确认",
        					handler:function(index,data,rowEle){
        						if(data.isSales == false){
        							Dialog.alert({
        								content: "该记录还未销账！"
        							});
        							return false;
        						}
        						if(data.status && data.status.key == "Confirm") {
        							Dialog.alert({
        								content: "该记录已确认！"
        							});
        							return false;
        						}else{
        							widget.set("confirmData", []);
        							widget.handler.gridHandler.confirmClick(data, widget);
        						}
        					}
        				},{
        					id:"check",
        					text:"查看",
        					handler:function(index,data,rowEle){
        							widget.handler.gridHandler.checkClick(data, widget);
        					}  
        				},{
        					id:"sales",
        					text:"销账",
        					show: function(value,data){
        						if(data.isSales == false){
        							return true;
        						}else{
        							return false;
        						}
        					},
        					handler:function(index,data,rowEle){
        						aw.ajax({
        							url : "api/checkoutsetlconfirm/changeoncredit",
        							data : {
        								pkCheckOutDeal : data.pkCheckOutDeal,
        								isSales : true,
        								money : data.checkInImplement.onCreditMoney + data.money,
        								fetchProperties : "*"
        							},
        							dataType : "json",
        							success : function(datas){
        								widget.get("grid").refresh();
        							}
        						})
        					}
        				}]
        			}]
        		}
        	});
        	widget.set("grid", grid);
        }
    };
    module.exports = instance;
});
